package com.compass.mobile.ui.viewModel

import LogUtil
import android.R.attr.type
import androidx.lifecycle.MutableLiveData
import com.compass.mobile.framework.utils.DateUtils
import com.compass.mobile.framework.utils.TipsToast
import com.compass.mobile.models.DepartmentBean
import com.compass.mobile.models.Doctor
import com.compass.mobile.models.EquityBean
import com.compass.mobile.models.InquiryInfo
import com.compass.mobile.models.Inspection
import com.compass.mobile.models.OrderBean
import com.compass.mobile.network.callback.IApiErrorCallback
import com.compass.mobile.network.manager.ApiManager
import com.compass.mobile.network.viewmodel.BaseViewModel
import com.google.gson.JsonObject
import stringListToJson
import toJson
import java.util.ArrayList

/**
 * Description：问诊
 * Date:2024/6/4 14:00
 */
class InquiryViewModel : BaseViewModel() {
    val departmentListLiveData = MutableLiveData<Map<String, MutableList<DepartmentBean>?>?>()
    val departmentLiveData = MutableLiveData<DepartmentBean?>()
    val consultationOrderLiveData = MutableLiveData<Any?>()
    val doctorsLiveData = MutableLiveData<MutableList<Doctor>?>()
    val benefitLiveData = MutableLiveData<MutableList<EquityBean>?>()
    val ordersLiveData = MutableLiveData<MutableList<OrderBean>?>()
    val inspectionLiveData = MutableLiveData<MutableList<Inspection>?>()
    val inspectionDetailsLiveData = MutableLiveData<Inspection?>()
    val orderDetailsLiveData = MutableLiveData<OrderBean>()
    val searchLiveData = MutableLiveData<MutableList<DepartmentBean>>()
    val allDepartmentListLiveData = MutableLiveData<MutableList<DepartmentBean>>() //所有部门数据
    val allDeptListLiveData = MutableLiveData<MutableList<DepartmentBean>>() //所有部门数据
    val submitInquiryInfoLiveData = MutableLiveData<String?>()
    val checkOrderBeanLiveData = MutableLiveData<OrderBean?>()

    fun getAllDeptList(name:String?,limit:Int?){
        launchUIWithResult(responseBlock = {
            ApiManager.api.geDeptList(name,limit)
        }, errorCall = object :IApiErrorCallback{
            override fun onError(code: Int?, error: String?) {
                super.onError(code, error)
            }
        }, successBlock = {
            if(it !=null){
                allDeptListLiveData.value = it
            }
        })
    }

    fun geDeptList() {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getLetterDeptList(null)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            if(it!=null){
                LogUtil.d(it.toJson())
            }
            departmentListLiveData.value = it
        })
    }

    /**
     * 1是图文 2是电话 3是视频
     */
//    fun getLastInquiryStatus(type:String,patientId: String) {
//        launchUIWithResult(responseBlock = {
//            ApiManager.api.getLastInquiryStatus(type,patientId)
//        }, errorCall = object : IApiErrorCallback {
//            override fun onError(code: Int?, error: String?) {
//                TipsToast.showTips(error)
//                benefitLiveData.value=null
//            }
//        }, successBlock = {
//            lastInquiryInfoLiveData.value =it
//        })
//    }
    /**
     * 1是图文 2是电话 3是视频
     */
    fun checkBenefit(type:String) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.checkBenefit(type)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                benefitLiveData.value=null
            }
        }, successBlock = {
            benefitLiveData.value=it
        })
    }

    /**
     * 检查订单状态
     */
//    fun checkOrderStatus(type: String,doctorId: String,patientId: String,action:String){
//        launchUIWithResult(responseBlock = {
//            ApiManager.api.checkOrderStatus(type, doctorId,patientId)
//        }, errorCall = object : IApiErrorCallback {
//            override fun onError(code: Int?, error: String?) {
//                TipsToast.showTips(error)
//                checkOrderBeanLiveData.value = null
////                benefitLiveData.value=null
//            }
//        }, successBlock = {
//            it?.let { detail ->{
//                    detail.action=action
//                    checkOrderBeanLiveData.value = detail
//                }
//            }
//        })
//    }

    fun checkOrderStatus(type: String,doctorId: String,patientId: String,action:String) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.checkOrderStatus(type, doctorId,patientId)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            if(it!=null){
                it.action=action
                checkOrderBeanLiveData.value = it
            }else{
                checkOrderBeanLiveData.value = null
            }
        })
    }

    fun gePatientOrderDetails(id:String?,action:String) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.gePatientOrderDetails(id)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            it?.let { details->
                details.action=action
                orderDetailsLiveData.value = details
            }

        })
    }

    fun geInspectionOrder(orderId:String?) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.geInspectionOrder(orderId)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            inspectionDetailsLiveData.value = it
        })
    }

    fun geDeptListTwo(pageNo: Int=1, pageSize: Int = 1000) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.geDeptListTwo(pageNo,pageSize)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            departmentListLiveData.value = it
        })
    }

    fun geDeptListTwoBySort(pageNo: Int=1, pageSize: Int = 1000,name: String?) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.geDeptListTwoBySort(pageNo,pageSize,name)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            allDepartmentListLiveData.value = it
        })
    }




    fun consultationOrder(orderId:String) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.consultationOrder(orderId)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {

            consultationOrderLiveData.value=it
        })
    }

    fun geDeptDetails(deptId: String?) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.geDeptDetails(deptId)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            departmentLiveData.value = it
        })
    }


    /**
     * @param type  1是查医生
     * 获取问诊医生
     */
    fun getInquiryDoctors(deptId: String?,type:Int=1, pageNo: Int, pageSize: Int = 4) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.geDoctors(deptId, type,pageNo, pageSize)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            if (it == null || it.list.isNullOrEmpty()) {
                doctorsLiveData.value = null
            } else {
                doctorsLiveData.value = it.list
            }
        })
    }

    /**
     * @param codeDept 科室编码code
     * @param onlyHaveSch 是否全查，0表示全查，1表示只查有排班的
     * @param onlyExpert  是否只查专家 ，0表示全查，1表示只查专家的
     */
    fun geRecommendDoctors(codeDept: String?,onlyHaveSch:Int=1, onlyExpert:Int=0,pageNo: Int, pageSize: Int = 4) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.geRecommendDoctors(codeDept, onlyHaveSch,onlyExpert,pageNo, pageSize)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            if (it == null || it.list.isNullOrEmpty()) {
                doctorsLiveData.value = null
            } else {
                doctorsLiveData.value = it.list
            }
        })
    }

    /**
     * 提交问诊人信息
     */
    fun submitCollaborationOrder(type: String,patientId: String,doctorId: String,departmentId:String,inquiryInfo: InquiryInfo){

        val params = JsonObject().apply {
            addProperty("type",type)
            addProperty("patientId",patientId)
            addProperty("doctorId",doctorId)
            addProperty("departmentId",departmentId)
            addProperty("ifReturnVisit",inquiryInfo.ifReturnVisit )
            addProperty("illnessDuration",inquiryInfo.illnessDuration)
            addProperty("lastDiagnosis", inquiryInfo.lastDiagnosis)
            addProperty("lastVisitHospital",inquiryInfo.lastVisitHospital )
            if(inquiryInfo.lastVisitDate?.isNotEmpty() == true){
                addProperty("lastVisitDate",DateUtils.formatDate(inquiryInfo.lastVisitDate,DateUtils.FORMAT_THREE))
            }
            addProperty("ifSanity", inquiryInfo.ifSanity)
            addProperty("inHospitalHere",inquiryInfo.inHospitalHere )
            addProperty("inpatientDepartment", inquiryInfo.inpatientDepartment)
            if(inquiryInfo.inpatientDate?.isNotEmpty() == true){
                addProperty("inpatientDate",DateUtils.formatDate(inquiryInfo.inpatientDate,DateUtils.FORMAT_THREE) )
            }
            addProperty("diseaseDescriptionText", inquiryInfo.diseaseDescriptionText)
            addProperty("openid","")
            addProperty("payChannel","wx_tv")
            add("diseaseDescription", stringListToJson(inquiryInfo.diseaseDescription))
//            addProperty("diseaseDescription", stringListToJson(diseaseDescription))
        }
        launchUIWithResult(responseBlock = {
            ApiManager.api.submitCollaboration(params)
        }, errorCall = object :IApiErrorCallback{
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                submitInquiryInfoLiveData.value = null
            }
        },successBlock = {
            submitInquiryInfoLiveData.value=it
        })
    }

    //获取问诊列表
    fun getConsultationList(patientId: String?, type: String?, pageNo: Int, pageSize: Int = 10) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getConsultationList(patientId, type, pageNo, pageSize)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            if (it == null || it.list.isNullOrEmpty()) {
                ordersLiveData.value=null
            } else {
                ordersLiveData.value = it.list
            }
        })
    }

    //获取检验检查列表
    fun getInspectionList(patientId: String?, type: String?, pageNo: Int, pageSize: Int = 10) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getInspectionList(patientId, type, pageNo, pageSize)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            if (it == null || it.list.isNullOrEmpty()) {
                inspectionLiveData.value=null
            } else {
                inspectionLiveData.value = it.list
            }
        })
    }


}
